home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / examples / doc / ptr_sort.pro < prev    next >
Text File  |  1997-07-08  |  2KB  |  75 lines

  1. ; PTR_SORT accepts one arugument, a pointer to the first element
  2. ; of a linked list returned by PTR_READ. Note that the PTR_SORT
  3. ; program does not need to know how many elements are in the list,
  4. ; nor does it need to explicitly know of any pointer other than the first.
  5.  
  6. pro ptr_sort, first
  7.  
  8. ; Initialize swap flag
  9.  
  10. swap = 1
  11.  
  12. ; Create an anonymous strucutre to contain list elements. Note that 
  13. ; the next field is initialized to be a pointer. Create a pointer to
  14. ; this structure, to be used as "swap space."
  15.  
  16. llist = {name:'', next:PTR_NEW()}
  17. junk = ptr_new(llist)
  18.  
  19. ; Continue the sorting until no swaps are made. If no adjacent 
  20. ; elements need to be swapped, the list is in alphabetical order.
  21.  
  22. WHILE swap NE 0 DO BEGIN
  23.  
  24.   ; Create a second pointer to the heap variable pointed at by _first_,
  25.   ; and another pointer to the heap variable held in the next field
  26.   ; of _current_.
  27.  
  28.   current = first
  29.   next = (*current).next
  30.   swap = 0
  31.  
  32.   ; Continue the sorting until next is no longer a valid pointer.
  33.   ; Note that the null pointer is not a valid pointer.
  34.  
  35.   WHILE PTR_VALID(next) DO BEGIN
  36.  
  37.     ; Get values to compare.
  38.  
  39.     value1 = (*current).name
  40.     value2 = (*next).name
  41.  
  42.     ; Compare values and exchange if first is greater than second.
  43.  
  44.     IF (value1 GT value2) THEN BEGIN
  45.  
  46.       ; Use the "swap space" pointer to exchange the name fields of
  47.       ; _current_ and _next_.
  48.  
  49.       (*junk).name = (*current).name
  50.       (*current).name = (*next).name
  51.       (*next).name = (*junk).name
  52.  
  53.       ; Set _current_ to _next_ to advance through the list.
  54.  
  55.       current = next
  56.  
  57.       ; Reset swap flag.
  58.  
  59.       swap = 1
  60.  
  61.     ; If value1 is less than value2, set _current_ to _next_
  62.     ; to advance through the list.
  63.  
  64.     ENDIF ELSE current = next
  65.  
  66.     ; Redefine _next_ pointer.
  67.  
  68.     next = (*current).next
  69.  
  70.   ENDWHILE
  71.  
  72. ENDWHILE
  73.  
  74. END
  75.